package classification;

import infrastructure.LoggingManager;

import java.util.Date;

import org.apache.log4j.Logger;

public class ProteinPredictionResult {
	
	private Logger logger = LoggingManager.getLogger();
	
	private final char IN_MENBRANE_SYMBOL = '+';
	private final char OUT_MENBRANE_SYMBOL = '-';
	
	private final double IN_MENBRANE_VALUE = 0.0;
	private final double OUT_MENBRANE_VALUE = 1.0;
	
	private String summary;
	private double [] classificationResults;
	private String[]  indexesProtein;
	private Date dateTime;
	
	private String formattedResult;
	
	
	public ProteinPredictionResult(String _summary, double [] _classificationResults, String [] indexesProtein) {
		this.summary = _summary;
		this.classificationResults = _classificationResults;
		this.indexesProtein = indexesProtein;
		this.dateTime = new Date();
		//0.2, 0.15, 1, "500");
	}
	
	public Date getDateTime() {
		return dateTime;
	}
	
	public double [] getClassificationResults() {
		return classificationResults;
	}
	
	public String getSummary() {
		return summary;
	}
	
	@Override 
	public String toString(){
		if (formattedResult == null){
			StringBuilder result = new StringBuilder();
			
			for (int i =0; i < this.classificationResults.length; i++){
				if (isNewId(i)){
					if (i==0) {
						result.append(">"+this.currentId+"\n");
					}
					else {
						result.append("\n" + ">"+this.currentId+"\n");
					}
				}
				
				if (this.classificationResults[i] == IN_MENBRANE_VALUE)
					result.append(IN_MENBRANE_SYMBOL);
				else if (this.classificationResults[i] == OUT_MENBRANE_VALUE)
					result.append(OUT_MENBRANE_SYMBOL);
				else {
					result.append('%');
					logger.error(String.format("Incorrect value in the results %f at %d", this.classificationResults[i], i));
					
				}
			}
			
			resetIndexesProtein();
			this.formattedResult = result.toString();
		}
		
		return formattedResult;
	}
	
	int indexForIndexesProtein = 0;
	int currentMaxIndexIndexesProtein = -1;
	int currentMinIndexIndexesProtein = 0;
	String currentId;
	private boolean isNewId(int indexToFind) {
		
		if ( indexToFind >= currentMinIndexIndexesProtein && indexToFind <= currentMaxIndexIndexesProtein ) {
			return false;
		}
		
		while (this.indexForIndexesProtein < this.indexesProtein.length) {
			String text = this.indexesProtein[this.indexForIndexesProtein];
			String [] splitResult = text.split(",");
			// Get Max Index
			String id = splitResult[0];
			currentMinIndexIndexesProtein = Integer.parseInt(splitResult[1]);
			currentMaxIndexIndexesProtein = Integer.parseInt(splitResult[2]);
			
			if ( indexToFind >= currentMinIndexIndexesProtein && indexToFind <= currentMaxIndexIndexesProtein ) {
				currentId = id;
				return true;
			}
			
			this.indexForIndexesProtein ++;
		}
		
		
		logger.error("ERROR: ID NOT FOUND. The indexes are not being validated correctly.");
		currentId = "ID NOT FOUND";
		return true;
	}
	
	private void resetIndexesProtein(){
		currentId = "";
		indexForIndexesProtein = 0;
		currentMaxIndexIndexesProtein = -1;
		currentMinIndexIndexesProtein = 0;
	}
}
